package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.util.TreeUtil;
import com.cxf.windex.base.vo.MenuTree;
import com.cxf.windex.entity.WeiMenu;
import com.cxf.windex.mapper.WeiMenuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenxf
 * @date 4/10/2018
 * @description
 */
@Service
public class WeiMenuBiz extends BaseBiz<WeiMenuMapper,WeiMenu> {
    /**
     * 获取菜单
     * @param type
     * @return
     */
    public List<MenuTree> getMenu(String type){
        Example example = new Example(WeiMenu.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(type)) {
            if (Integer.valueOf(type) < 4) {
                criteria.andLessThanOrEqualTo("type", type);
            } else {
                criteria.andEqualTo("type", type);
            }
        }
        example.setOrderByClause(" order_num asc");
        List<WeiMenu> menus = selectByExample(example);
        return getMenuTree(menus, 0L);

    }

    private List<MenuTree> getMenuTree(List<WeiMenu> menus, long root) {
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        for (WeiMenu menu : menus) {
            node = new MenuTree();
            BeanUtils.copyProperties(menu, node);
            trees.add(node);
        }
        return TreeUtil.buildByRecursive(trees, root);
    }
}
